import java.net.URL;
import java.util.Vector;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;


public class ClientToServer {
	private static XmlRpcClientConfigImpl config;
	private static XmlRpcClient client;
	
	/**
	 * register client in server
	 */
	public static void registerInServer(){
		try {
		     config = new XmlRpcClientConfigImpl();
		     config.setServerURL(new URL("http://" + XMLUtil.serverIP + ":" + XMLUtil.serverPort));
		     client = new XmlRpcClient(); 
		     client.setConfig(config);
		     Vector<String> params = new Vector<String>();
		     params.addElement(XMLUtil.clientIP);
		     params.addElement(XMLUtil.clientPort);
		    client.execute("server.register", params);
		     
		   } catch (Exception exception) {
		     System.err.println("JavaClient: " + exception);
		   }
	}
	
	public static void disconnect(){
		Vector<String> params = new Vector<String>();
		params.addElement(XMLUtil.clientIP);
	    params.addElement(XMLUtil.clientPort);
	    try {
			client.execute("server.disconnect", params);
		} catch (XmlRpcException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 
	 * @param value upate interval time;
	 */
	public static void setInterval(int value){
		Vector params = new Vector();
		params.addElement(XMLUtil.clientIP);
		params.addElement(XMLUtil.clientPort);
		params.addElement(Integer.valueOf(value));
		try {
			client.execute("server.updateInterval",params);
		} catch (XmlRpcException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
